#ifndef __COMMON__
#define __COMMON__

#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include <fcntl.h>
#include "log.hpp"

#define FIFO_FILE "./fifo"
#define MODE 0664

enum {
    FIFO_CREAT_ERR = 1,
    FIFO_DELET_ERR,
    FIFO_OPEN_ERR
};

//using namespace std;
const char *pathname = "./dir";
const int proj = 123;

Log log;
key_t getkey()
{
    key_t key = ftok(pathname, proj);
    if (key < 0)
    {
        log.logmessage(fatal, "ftok error: %s", strerror(errno));
    }

    printf("key:%x\n", key);
    return key;
}

int shm(int flag)
{
    key_t key = getkey();
    int shmid = shmget(key, 4096, flag);
    if (shmid < 0)
    {
        log.logmessage(fatal, "shmget error: %s", strerror(errno));
    }
    printf("shmid:%x\n", shmid);

    return shmid;
}
//创建
int creatshm()
{
    return shm(IPC_CREAT | IPC_EXCL | 0666);
}

//获取
int getshm()
{
    return shm(IPC_CREAT);
}


class Init
{
public:
    Init()
    {
        // 创建管道
        int n = mkfifo(FIFO_FILE, MODE);
        if (n == -1)
        {
            perror("server creat");
            exit(FIFO_CREAT_ERR);
        }
    }

    ~Init()
    {
        // 删除管道
        int n = unlink(FIFO_FILE);
        if (n == -1)
        {
            perror("server unlink");
            exit(FIFO_DELET_ERR);
        }
    }
};

#endif